function [h  flag] = Channel_Estimation_General_New(Input,Reference,t_const,Oversample,HF_SRRC,Expected_NPaths)

%*-------------------------------- CHANNEL ESTIMATION ------------------------------------------*%
%
%  [h  flag] = Channel_Estimation(Input,t_const,Oversample,HF_SRRC,Expected_NPaths)
% 
% Channel_Estimation function to estimate the channel effect from the
% training symbols (Input_Miniprobes), while the t_const is the max delay
% spread for the channel.
%
% Expected_NPaths : the expected number of paths.
% 
% the Input is a matrix contains the miniprobes samples preceded
% by a number of samples relative to the t_const and followed by a the same
% number of samples.
%
% the output h is a matrix, while each row represents the channel response
% at this minprobe number.
%
% flag output : flag for the indecies of the different paths.
%
%*--------------------------------------------------------------------------------------------------*%


load preamble
Rate_8PSK = 4800;
[L W] = size(Input);
Mini_Probe_Plus_Modulated =  HF_Modulate(Mini_Probe_Plus,Rate_8PSK);
Mini_Probe_Plus_Upsampled = upsample(Mini_Probe_Plus_Modulated,Oversample);
max_delay_spread = Expected_NPaths;  % in symbols
% Reference = -Mini_Probe_Plus_Upsampled;


h= zeros(L,max_delay_spread);
h_indecies= ones(L,max_delay_spread);


for ch_index = 1:L
    
    yy = conv(conj(fliplr(Reference)),Input(ch_index,:));
    
    peak_range = fix(length(yy)/2) - Oversample : fix(length(yy)/2) + (max_delay_spread+2);
    
     % plot
%     plot(abs(yy/length(Mini_Probe_Plus))),hold,grid
%     plot(peak_range,abs(yy(peak_range))/length(Mini_Probe_Plus),'r.')
%     plot(peak_range,real(yy(peak_range))/length(Mini_Probe_Plus),'k')
%     plot(peak_range,imag(yy(peak_range))/length(Mini_Probe_Plus),'m')
    %%%%%%%
    
    [indecies N_Paths] = GetPeak_Indecies(abs(yy(peak_range)),0,max_delay_spread);
    
    h_indecies(ch_index,1:N_Paths) = indecies+fix(length(yy)/2)-Oversample-1;
    
    [h(ch_index,1:N_Paths)] = GetPeak_Value(yy/length(Mini_Probe_Plus),h_indecies(ch_index,1:N_Paths),HF_SRRC);
    
%     h2(ch_index,:) = yy( h_indecies(ch_index,1:N_Paths))/length(Mini_Probe_Plus);
    
    flag(ch_index,:) = h_indecies(ch_index,:) - t_const - length(Mini_Probe_Plus)*Oversample;
   
%     close

end








